'Copyright (c) 2008, Jonathan Allen
Imports System.Net


Public Class NetflixException
	Inherits System.Exception

	Public Sub New(ByVal message As String, ByVal innerException As Net.WebException)
		MyBase.New(message, innerException)
	End Sub

	Public Shared Function ParseException(ByVal baseException As Net.WebException) As Exception
		Dim response = DirectCast(baseException.Response, Net.HttpWebResponse)
		If response Is Nothing Then Return baseException 'this is a generic network error, not a Netflix Error

		Dim data = New IO.StreamReader(response.GetResponseStream).ReadToEnd

		Select Case response.StatusCode
			Case Net.HttpStatusCode.Forbidden
				If data.IndexOf("Over queries per second limit", StringComparison.CurrentCultureIgnoreCase) >= 0 Then
					Return New RequestFrequencyException(data, baseException)
				ElseIf data.IndexOf("Over queries per day limit", StringComparison.CurrentCultureIgnoreCase) >= 0 Then
					Return New DailyRequestCountException(data, baseException)
				End If

			Case HttpStatusCode.NotFound
				Return New NotFoundException(data, baseException)
		End Select

		'default case
		Return New NetflixException(data, baseException)
	End Function

End Class
